﻿$(document).ready(function () {
    $('#Country').change(function () {
        stateValidation();
        var selectedProvinceId = $(this).val();
        $.ajax({
            url: '/IdentificationInformation/GovtIdType/',
            //url: base_url + '/IdentificationInformation/GovtIdType/',
            data: { CountryId: selectedProvinceId }, //parameters go here in object literal form 
            type: 'GET',
            datatype: 'json',
            success: function (GovtIdTypeIds) {
                govtidselect = $('#GovtIDType');
                govtidselect.empty();
                var items = '<option>Select</option>';
                $.each(GovtIdTypeIds, function (i, state) {
                    items += "<option value='" + state.Value + "'>" + state.Text + "</option>";
                    // state.Value cannot contain ' character. We are OK because state.Value = cnt++;
                });
                $('#GovtIDType').html(items);
            },
            error: function () { alert('Error processing JSON Call for Government ID Type'); }
        });
    });
    $('#GovtIDType').change(function () {
        var selectedProvinceId = $(this).val();
        var selectedCountryId = $('#Country').val();
        stateValidation();
        $.ajax({
            url: '/IdentificationInformation/GetStates/',
            //url: base_url + '/IdentificationInformation/GetStates/',
            data: { CountryId: selectedCountryId, GovtIdType: selectedProvinceId }, //parameters go here in object literal form 
            type: 'GET',
            datatype: 'json',
            success: function (States) {
                govtidselect = $('#GovtIdIssueState');
                govtidselect.empty();
                var items = '<option>Select</option>';
                $.each(States, function (i, state) {
                    items += "<option value='" + state.Value + "'>" + state.Text + "</option>";
                    // state.Value cannot contain ' character. We are OK because state.Value = cnt++;
                });
                $('#GovtIdIssueState').html(items);
            },
            error: function () { alert('Error processing JSON Call for Government ID Type'); }
        });
    });

    function stateValidation() {
        var govtIdType = $('#GovtIDType').val();
        var countryId = $('#Country').val();
        if ((countryId.toUpperCase() == 'UNITED STATES' && govtIdType.toUpperCase() == "DRIVER'S LICENSE") || (countryId.toUpperCase() == 'MEXICO' && govtIdType.toUpperCase() == 'LICENCIA DE CONDUCIR'))
        { $('#IsStateMandatory').show(); $('#GovtIdIssueState').attr('data-val', true); }
        else
        { $('#IsStateMandatory').hide(); $('#GovtIdIssueState').attr('data-val', false); }
    }
});